/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.binary;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.exception.SematicException;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.data_type.DataType;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionNode;

public class ConditionalAndNode extends BinaryExpressionNode {
	public ConditionalAndNode(ExpressionNode condAnd, 
								ExpressionNode expr) {
		this.left=condAnd;
		this.rigth=expr;
	}
	
	public DataType getType() throws SematicException {
		if(expressionType!=null)
			return expressionType;
		
		DataType dt1=this.left.getType();
		DataType dt2=this.rigth.getType();
		
		if((dt1==DataType.INT || dt1==DataType.SHORT)
				&& (dt2==DataType.INT || dt2==DataType.SHORT)) 
		{
			expressionType=DataType.SHORT;
		}
		else
		{
			throw new SematicException("Conditional AND opperation work with numeric types.");
		}
		return expressionType;
	}
	
	@Override
	public void generateTAC() throws IOException {
		this.left.generateTAC();
		int a=tacCounter.getTermIndex()-1;
		this.rigth.generateTAC();
		int b=tacCounter.getTermIndex()-1;
	
		this.writeTAC("&&", "_t"+a, "_t"+b, "_t"+tacCounter.getNextTermIndex());		
	}
}
